package com.wangcang.widgetlibrary.recording;

import android.app.Dialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.dialog.DialogTools;
import com.wangcang.widgetlibrary.tools.LogUtils;

import java.io.File;
import java.io.IOException;

public class RecordingPlayActivity extends AppCompatActivity implements View.OnClickListener {
    private static int code = 1000;
    private Button mActivityRecodingReadBtn;
    private Button mActivityRecodingPlayBtn;
    private Button mActivityRecodingDeleteBtn;

    private String recordingFilepath = null;
    private MediaRecorder mMediaRecorder = null;
    private RecordArmUtils mRecordArmUtils = null;

    private boolean isPlayingOrnot = false;
    private TextView playDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recording_play);
        initView();
    }

    private void initView() {
        mMediaRecorder = new MediaRecorder();
        mRecordArmUtils = new RecordArmUtils(this, mMediaRecorder);

        mActivityRecodingReadBtn = (Button) findViewById(R.id.activity_recoding_read_btn);
        mActivityRecodingPlayBtn = (Button) findViewById(R.id.activity_recoding_play_btn);
        mActivityRecodingDeleteBtn = (Button) findViewById(R.id.activity_recoding_delete_btn);
        playDuration = (TextView)findViewById(R.id.activity_recoding_play_duration_txt);

        mActivityRecodingReadBtn.setOnClickListener(this);
        mActivityRecodingPlayBtn.setOnClickListener(this);
        mActivityRecodingDeleteBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        if(v.getId() == R.id.activity_recoding_read_btn){
            recordingFilepath = null;
            intent.setClass(this,RecordingActivity.class);
            startActivityForResult(intent, code);
        }else if(v.getId() == R.id.activity_recoding_play_btn){


            if(recordingFilepath !=null){

                if(isPlayingOrnot){//在播放
                    showPlayorNot(isPlayingOrnot);
                    mRecordArmUtils.stop();
                }else{
                    showPlayorNot(isPlayingOrnot);
                    try {
                        mRecordArmUtils.playRecordVolume(recordingFilepath, new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                showPlayorNot(isPlayingOrnot);
                            }
                        });
                    } catch (IOException e) {
                        showPlayorNot(isPlayingOrnot);
                        e.printStackTrace();
                    }
                }



            }


        }else if(v.getId() == R.id.activity_recoding_delete_btn){
            if(recordingFilepath !=null){

                DialogTools.getInstance().showCustomDialogPrompt(this, "Tip", "你确定要删除录音吗？", false, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String input) {
                        File file  = new File(recordingFilepath);
                        if(file.isFile()) {
                            file.delete();
                        }
                        recordingFilepath = null;
                        playDuration.setText(toTime(-1));
                        dialog.dismiss();
                    }
                });



            }
        }
    }

    public void showPlayorNot(boolean temp){
        if(temp){
            mActivityRecodingPlayBtn.setBackground(getResources().getDrawable(R.drawable.common_recording_play_selector));
            isPlayingOrnot = false;
        }else{
            mActivityRecodingPlayBtn.setBackground(getResources().getDrawable(R.drawable.common_recording_stop_selector));
            isPlayingOrnot = true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //录音回调地方。
        if(code==resultCode) {
            Bundle bundle = data.getExtras();
            recordingFilepath = bundle.getString("filepath");
            playDuration.setText(toTime(mRecordArmUtils.getPlayDuration(recordingFilepath)));
            LogUtils.showErrLog("recordingFilepath     " + recordingFilepath);
        }
    }


    public String toTime(long miSecond){
        StringBuilder result = new StringBuilder("");
        if(miSecond == -1){
            return "00:00";
        }

        int second  = (int)miSecond/1000;
        if(second/60 >= 10){
            result.append(second/60+":");
        }else{
            result.append("0" + second/60+":");
        }

        if(second%60 >= 10){
            result.append(second%60+"");
        }else{
            result.append("0" + second%60+"");
        }
        return result.toString();
    }


    @Override
    protected void onPause() {
        super.onPause();
        mRecordArmUtils.stop();
        if(isPlayingOrnot){
            showPlayorNot(isPlayingOrnot);
        }
    }

}



